﻿<%@ WebHandler Language="C#" Class="ZSRegularCheckinfoReport" %>

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Net;
using ReportBusiness;

/**
 * Description: 装配试车厂设备定检查询报表
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * Date:2022.11.24
 **/

public class ZSRegularCheckinfoReport : IHttpHandler
{
    uMESReportOracleDAL dal = new uMESReportOracleDAL();
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                Result r = new Result();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "getData":
                        r = getData(jObject);//查询
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "exportExcel":
                        exportExcel(jObject);//导出
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    string getQuerySql(JObject jObject)
    {
        string sql = @"SELECT * FROM t_resourceregularcheckinfo t WHERE 1=1 ";

        //设备编码
        if (jObject["equnr"] != null && jObject["equnr"].ToString() != "")
        {
            sql += string.Format(" AND t.equnr LIKE '%{0}%' ", jObject["equnr"].ToString());
        }

        //设备名称
        if (jObject["eqktx"] != null && jObject["eqktx"].ToString() != "")
        {
            sql += string.Format(" AND t.eqktx LIKE '%{0}%' ", jObject["eqktx"].ToString());
        }

        //型号
        if (jObject["typbz"] != null && jObject["typbz"].ToString() != "")
        {
            sql += string.Format(" AND t.typbz LIKE '%{0}%' ", jObject["typbz"].ToString());
        }

        //检定日期
        if (jObject["selectdate"] != null && jObject["selectdate"].ToString() != "")
        {
            JArray arr = JArray.Parse(jObject["selectdate"].ToString());
            if (arr.Count >= 2)
            {
                sql += string.Format(" AND TRUNC(t.zjdrq) >= to_date('{0}','yyyy/mm/dd')", arr[0].ToString());
                sql += string.Format(" AND TRUNC(t.zjdrq) <= to_date('{0}','yyyy/mm/dd')", arr[1].ToString());
            }
        }
        return sql;
    }

    //查询方法
    Result getData(JObject jObject)
    {
        string sql = getQuerySql(jObject);
        uMESPagingDataDTO dto = dal.GetPagingDataIns(sql, (int)jObject["currentPage"], (int)jObject["pageSize"]);
        DataTable dt = dto.DBTable;
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        Result r = new Result();
        r.data = dt;
        r.total = Convert.ToInt32(dto.RowCount);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    //导出
    void exportExcel(JObject jObject)
    {
        string sql = getQuerySql(jObject);
        DataTable dt = dal.GetDataTable(sql);
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        dt.Columns.Remove("status");
        dt.Columns.Remove("workcenterid");
        dt.Columns.Remove("workcentername");

        dt.Columns["equnr"].Caption = "设备编码";
        dt.Columns["eqktx"].Caption = "设备名称";
        dt.Columns["begru"].Caption = "资产管理部门";
        dt.Columns["eqtyp"].Caption = "设备种类";
        dt.Columns["eqart"].Caption = "设备分类";
        dt.Columns["groes"].Caption = "规格";
        dt.Columns["ansdt"].Caption = "出厂日期";
        dt.Columns["typbz"].Caption = "型号";
        dt.Columns["indbt"].Caption = "启用日期";
        dt.Columns["zyxq"].Caption = "有效期";
        dt.Columns["zjdbm"].Caption = "定检部门";
        dt.Columns["zjdrq"].Caption = "定检日期";
        dt.Columns["zjdzq"].Caption = "检定周期";
        dt.Columns["zzqdw"].Caption = "检定周期单位";
        dt.Columns["zckusr"].Caption = "检定员";
        dt.Columns["zzqdj"].Caption = "精度等级";
        ExportHelper.ExportExcel(dt, jObject["factoryname"].ToString() + "设备定检信息查询报表");
    }
}